<?php

namespace App\Http\Controllers\common;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Controllers\common\TourlController;

class YunpianController extends Controller
{
    protected $baseUri;
    protected $apiKey;
    protected $callBack_url;

    public function __construct()
    {
        $this->baseUri = 'https://yunpian.com/v2';
        $this->apiKey = '50ab0eeae01191319124586d8deacbce';
        // $this->$callBack_url = '';
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return   $response string to json
     */
    protected function send($path, $params=[])
    {
        if (isset($this->apiKey)) {
            $params['apikey'] = $this->apiKey;
        }
        if (! empty($callBack_url)) {
            $params['callback_url'] = $this->$callBack_url;
        }
        $url = $this->baseUri.'/'.$path;
        $response = TourlController::to_url($url, http_build_query($params));
        return $response;
    }

    /**
    * @param    $phone  string
    * @param    $text   string
    * @return   $response string to json
    * e.g.
    *   $phone '12345678910'      '12345678910,01234567891' '12345678910,01234567891'
    *   $text  '您的验证码是1234' '您的验证码是1234'        '您的验证码是1234,您的验证码是1234'
    */
    public function Msgsend($phone, $text)
    {
        $params['mobile'] = $phone;
        $params['text'] = $text;
        $phones = explode(',',$phone);
        end($phones);
        if (key($phones) > 0) {
            $texts = explode(',', $text);
            end($texts);
            if (key($texts) > 0) {
                if (key($phones) == key($texts)) {
                    $path = 'sms/multi_send.json';
                } else {
                    $response = '手机号码与短信内容数量不匹配！';
                }
            } else {
                $path = 'sms/batch_send.json';
            }
        } else {
            $path = 'sms/single_send.json';
        }
        if (isset($path)) {
            $response = $this->send($path,$params);
        }
        return $response;
    }
}